package com.ymatou.iapi.shoppingcartservice.testcase;

import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.shoppingcartservice.parameter.ShoppingcartProductBean;
import com.ymatou.iapi.shoppingcartservice.parameter.ShoppingcartProductGetInfoByIdReq;
import com.ymatou.iapi.shoppingcartservice.service.ShoppingcartProductGetInfoByIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.BuyerShoppingCartService;
import com.ymttest.business.service.ProductManagerCallService;
import com.ymttest.database.mongo.YmtShoppingCartWapper;
import com.ymttest.utils.EnvSetup;

public class Ts_ShoppingcartProductGetInfoById {
	
	private static ShoppingcartProductGetInfoByIdCall shoppingcartProductGetInfoByIdCall = new ShoppingcartProductGetInfoByIdCall();
	private static Integer userId = Integer.valueOf(EnvSetup.getData("shoppingcartUserId"));
	private static BuyerShoppingCartService buyerShoppingCartService = new BuyerShoppingCartService();

	YmtShoppingCartWapper ymtShoppingCartWapper = new YmtShoppingCartWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据购物车id获取购物车商品信息");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(shoppingcartProductGetInfoByIdCall.getOpurl());
	}	
	
	@After
	public void caseDown() {
		Logger.end();
		try {
			ymtShoppingCartWapper.deleteByUserId(userId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoById_001() throws Exception {
		Logger.start(true,"根据shoppingcartid获取购物车商品信息");
		try{
			Map<String, Object> productDB = buyerShoppingCartService.getShoppingCartProduct(userId);
			ShoppingcartProductGetInfoByIdReq req = new ShoppingcartProductGetInfoByIdReq();
			
			req.setShoppingCartId(String.valueOf(productDB.get("_id")));
			
			JSONObject ret = shoppingcartProductGetInfoByIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			JSONObject o = ret.getJSONObject("data");
			ShoppingcartProductBean beana = ProductManagerCallService.getBeanFromJson(o, ShoppingcartProductBean.class);

			Logger.verifyEquals(200, code, "接口code返回异常");
			buyerShoppingCartService.checkShoppingcartProductBeanFromDB(beana);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoById_002() throws Exception {
		Logger.start(true,"输入错误的shoppingcartid获取购物车商品信息");
		try{
			buyerShoppingCartService.getShoppingCartProduct(userId);
			ShoppingcartProductGetInfoByIdReq req = new ShoppingcartProductGetInfoByIdReq();

			req.setShoppingCartId("1234213asd");
			
			JSONObject ret = shoppingcartProductGetInfoByIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");

			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals("null", ret.get("data").toString(), "data数据返回异常");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductGetInfoById_003() throws Exception {
		Logger.start(true,"不传入shoppingcartid");
		try{
			buyerShoppingCartService.getShoppingCartProduct(userId, 1);
			ShoppingcartProductGetInfoByIdReq req = new ShoppingcartProductGetInfoByIdReq();
			
			req.setShoppingCartId("");
			
			JSONObject ret = shoppingcartProductGetInfoByIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("shoppingCartId:shoppingCartId不能为空;", message, "不传入shoppingcartid提示错误");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
}
